package gson.emlog.activity;

import gson.emlog.Emlog;
import gson.emlog.R;
import gson.emlog.utils.Constant;

import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 设置界面
 * @author GodSon
 *
 */
public class Setting extends Activity {
	private EditText blogUrl;
	private EditText appKey;
	private EditText clientKey;
	/*private CheckBox async;*/
	private SharedPreferences settings;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		Button saveBtn = (Button) findViewById(R.id.setting_save);
		saveBtn.setOnClickListener(click);
		settings = getSharedPreferences(Constant.PREFS_NAME, 0);  
		blogUrl = (EditText) findViewById(R.id.setting_blog_url);
		appKey = (EditText) findViewById(R.id.setting_blog_app_key);
		clientKey = (EditText) findViewById(R.id.setting_blog_client_key);
		/*async = (CheckBox) findViewById(R.id.setting_blog_async);*/
		
		blogUrl.setText(settings.getString(Constant.PREFS_KEY_BLOG_URL, ""));
		appKey.setText(settings.getString(Constant.PREFS_KEY_APP_KEY, ""));
		clientKey.setText(settings.getString(Constant.PREFS_KEY_CLIENT_KEY, UUID.randomUUID().toString().replace("-", "")));
		/*async.setChecked(settings.getBoolean(Constant.PREFS_KEY_ASYNC, false));*/
	}
	
	private View.OnClickListener  click = new View.OnClickListener(){
		public void onClick(View v) {
			
			SharedPreferences.Editor editor = settings.edit();  
			String url = blogUrl.getText().toString().trim();
	        String appkey =  appKey.getText().toString().trim();
	        String clintkey = clientKey.getText().toString().trim();
	        
	        if(url.equals("")|| appkey.equals("") || clintkey.equals("")){
	        	Toast.makeText(Setting.this, "保存失败，设置不完整!", Toast.LENGTH_SHORT).show();
	        }else{
	        	if(!match(url)){
	        		Toast.makeText(Setting.this, "博客地址错误！", Toast.LENGTH_SHORT).show();
	        	}else{
		        	editor.putString(Constant.PREFS_KEY_BLOG_URL,url.toLowerCase());
					editor.putString(Constant.PREFS_KEY_APP_KEY, appkey);
					editor.putString(Constant.PREFS_KEY_CLIENT_KEY, clintkey);
					/*editor.putBoolean(Constant.PREFS_KEY_ASYNC, async.isChecked());*/
					if(editor.commit()){
						Toast.makeText(Setting.this, "保存成功!", Toast.LENGTH_SHORT).show();
						startActivity(new Intent(Setting.this,Emlog.class));
						Setting.this.finish();
					}else{
						Toast.makeText(Setting.this, "保存失败!", Toast.LENGTH_SHORT).show();
					}
	        	}
	        }
		}
	};
	
	private boolean match(String str ){
		String regex = "http://(([a-zA-z0-9]|-){1,}\\.){1,}[a-zA-z0-9]{1,}-*" ;
        Pattern pattern = Pattern.compile(regex);
        Matcher  matcher = pattern.matcher( str );
        return matcher.matches();
    }
}
